<%@page import="org.apache.commons.httpclient.HttpClient,
         org.apache.jackrabbit.util.Text,
        org.apache.commons.httpclient.methods.*,
        java.io.* "   contentType="text/html;charset=UTF-8" %>
<% String url = request.getParameter("url");

   String useragent = request.getHeader("User-Agent");
   String user = useragent.toLowerCase();
   boolean ie = false;
   if(user.indexOf("msie") >=0) {
			ie = true;
    }
    String q = request.getParameter("q");
    if (url != null || url.trim().length() != 0) {
            if (url.toLowerCase().indexOf(".rtf") > 0) {

%>
    <script type="text/JavaScript" src ="http://www.java.com/js/deployJava.js" > </script>
     <script type="text/JavaScript" src ="js/jquery-1.4.3.min.js" > </script>
    <script type="text/JavaScript" >
    error = 0;
    // showApplet();
    </script>
    <% if (ie) { %>
        <object id="activeXId" classid="CLSID:9e157775-6909-3e8e-8112-b0dfbddb3ba9" style="margin:0px"
         height="95%" width="100%" onerror="error=1;">
        <param name="Query" value="<%= q%>"/>
      <B> <font size="-1"> Install <a href="RTFViewerSetup.msi">Embeded RTFViewer/Editor </a> and Restart your browser for MUCH BETTER display</font> </B>
    <br/>
</object>
    <% } %>
<script type="text/JavaScript" >
    if (error || !<%= ie %>)
    {
        var attributes = {codebase:'.',
            code:'com.lookinto.applet.RTFApplet.class',
            archive:'RTFApplet.jar',
            width:710, height:540, id:'appletId'} ;
        var parameters = {url:'<%=url%>',query:'<%=q%>'} ;
        var version = '1.6' ;
        // alert(1);
        deployJava.runApplet(attributes, parameters, version);
    }
   $.ajax({ url: "<%= url%>", /*username:'admin', password:'admin',*/ success: function(data) {
     if (document.getElementById('appletId') != null)
          document.getElementById('appletId').setRtf(data);
      if (document.getElementById('activeXId') != null)
          document.getElementById('activeXId').TextVal = data;
     }
   });

</script>
<%
      } else if (url.toLowerCase().indexOf(".pdf") > 0) {
         response.sendRedirect(url + "#search=%22" + q + "%22");
        } else if (url.toLowerCase().indexOf(".html") > 0) {
          //System.out.println(j.encode(url, "utf-8"));
           url = url.replaceAll(" ", "%20");
          //java.net.URL tempURL = new java.net.URL(url);
           HttpClient hc = new HttpClient();
           GetMethod pm = new GetMethod(url);
           hc.executeMethod(pm);

           BufferedReader br = new BufferedReader(new InputStreamReader(pm.getResponseBodyAsStream(), "UTF8"));
           String line;
           while ((line = br.readLine()) != null) {
                  out.println(line);
           }
             //IOUtils.copy(pm.getResponseBodyAsStream(), response.getOutputStream())
              out.println("<script type='text/JavaScript'> parent.highlightSearchTerms('" + q + "', false, true, null, null, document);</script> ");
           }
            }%>
      <!--
            <button style="display:none" onclick="alert(document.getElementById('myId').getRtf())" > test </button> -->